/*
 * @Author: 李景福|zhourunyu1@outlook.com(zhourunyu)
 * @Date: 2022-02-12
 * @LastEditors: 周润玉|zhourunyu1@outlook.com(zhourunyu)
 * @LastEditTime: 2022-07-25 08:58:14
 * @Description: 
 * @Administrator: 丁平|1572804782@qq.com(dingping)
 * @Copyright: 1.0.2
 */

import WorkReportMainApi from '../../../../../../public/js/common/api/mybusiness/workreport/workreportmain.api.js'
import { requestFilePath } from '../../../../../../public/js/common/api/file-request.js'
import watermark from '../../../../../../public/js/common/watermark.js';
import WxcodeApi from '../../../../../../public/js/common/api/account/wxcode.api.js';
/**
 *@file 汇报工作详情
 *@Date :2022-02-16 13:26:58
 *@create李景福
*/
(function (window, document, $) {
    $(function () {
        getId();
        refreshTitle();
        watermarkBgImg();
    });
    const watermarkBgImg = async () => {
        let _userInfo = JSON.parse(Utils.GetSessionStorage("SNAccountEply")) || {};
        if (Utils.IsNotNull(_userInfo)) {
            watermark.set((_userInfo.name || "") + _userInfo.numCode);// 水印

        }
        // if (!Utils.IsNotNull(_userInfo)) {
        //     let _json = await WxcodeApi.queryUserName();
        //     Utils.SetSessionStorage("SNLoginUserInfo", _json);
        //     return WorkReportMainApi.getLoginUserToStaff(Utils.GetQueryString('state') || '').then(res => {
        //         watermark.set((_json.curUserName || "") + res.code);// 水印
        //     }).catch(_ => {
        //         watermark.set(_json.curUserName || "");// 水印
        //     });
        // }
        // EmployeeMgtApi.getLoginUserToStaff(Utils.GetQueryString('state') || '').then(res => {
        //     watermark.set((_userInfo.curUserName || "") + res.code);// 水印
        // }).catch(_ => {
        //     watermark.set(_userInfo.curUserName || "");// 水印
        // });
    }

    const getData = (id) => {
        WorkReportMainApi.getWkrptsBillDetail(id).then(res => {
            const data = res;
            let { reportStartDate, reportEndDate, reportType } = data
            let _contentTitle = $(".contentTitle"), _planTitle = $(".planTitle")
            switch (data.reportType) {
                case 1: $('title').html("日报详情"); break;
                case 2: $('title').html("周报详情"); break;
                case 3: $('title').html("月报详情"); break;
                case 4: $('title').html("汇报详情"); break;
                default: $('title').html("详情"); break;
            };

            let _titleContent = {// 需要修改的标题 或者 文本域的提示内容
                1: { content: "今日工作", plan: "明日计划", setTime: moment(reportEndDate).format('YYYY-MM-DD') },
                2: { content: "本周工作", plan: "下周计划", setTime: moment(reportEndDate).format('YYYY-MM-DD') + `(第 ${moment(reportEndDate, "YYYY-MM-DD").isoWeek()} 周)` },
                3: { content: "本月工作", plan: "下月计划", setTime: moment(reportEndDate).format('YYYY-MM') },
                4: { content: "工作内容", plan: "下阶段计划", setTime: moment(reportStartDate).format('YYYY-MM-DD') + ' 至 ' + moment(reportEndDate).format('YYYY-MM-DD') }
            }
            let { content, plan, setTime } = _titleContent[reportType];
            _contentTitle.text(content);
            _planTitle.text(plan)


            $("#main-top-name").text(`${data.reporterName}的工作汇报`);
            $("#time").text(setTime);
            $("#todayWork").html(data.contents.replace(/\n/g, "<br>"));
            $("#plans").html(data.plans.replace(/\n/g, "<br>"));
            if (data.attachments.length > 0) {
                for (const item of data.attachments) {
                    if (item.suffix == 'jpg' || item.suffix == 'png' || item.suffix == 'ico' || item.suffix == 'bpm' || item.suffix == 'gif' || item.suffix == 'jpeg') {
                        $("#attachment").append(`<a class = "imgId" img-Id = "${item.id}" >
                <i class="icon iconfont icon-snh6-entity-image icon-font-size-16" style="font-size: 16px;" aria-hidden="true"></i>&nbsp;${item.name}</a><br>`)
                    } else {
                        $("#attachment").append(`<a class = "fileId" file-Id = "${item.id}">
                <i class="icon iconfont icon-snh6-file-download icon-font-size-16" style="font-size: 16px;" aria-hidden="true"></i>&nbsp;${item.name}</a><br>`)
                    }
                }
            } else {
                $("#attachment").text("无");
            }

            for (let i = 0; i < data.recipients.length; i++) {
                if (data.recipients.length === 0 || data.recipients.length === i + 1) {
                    $("#reportTo").append(`${data.recipients[i].name}`);
                } else {
                    $("#reportTo").append(`${data.recipients[i].name}、`);
                }

            }
        })
    };
    //TODO : 获取ID
    //Date :2022-02-16 13:15:32
    //create李景福
    const getId = () => {
        let urlParsm = Utils.ParsingUrl(window.location);//url.split("?id=");  // 获取url携带的参数    
        getData(urlParsm.id);//传入id查看
    }
    //图片下载
    $('#attachment').on('click', '.imgId', function () {
        const id = $(this).attr('img-Id');
        requestFilePath(id).then(res => {
            //   console.log('tupian',res.data)
            //   $(this).attr('href',res.data)
            $.photoBrowser({
                items: [res.data]
            }).open();

        })
    })
    //文件下载
    $('#attachment').on('click', '.fileId', function () {
        const id = $(this).attr('file-Id');
        requestFilePath(id).then(res => {
            window.location.href = `${res.data}`;
        })
    });

    //标题刷新
    const refreshTitle = () => {
        var $body = $('body');
        var $iframe = $('<iframe src="/favicon.ico"></iframe>');
        $iframe.on('load', function () {
            setTimeout(function () {
                $iframe.off('load').remove();
            }, 0);
        }).appendTo($body);
    };

    //TODO : 详情界面添加返回按钮
    //Date :2022-03-03 16:27:33
    //create张双磊
    $('#returnList').on('click', function () {
        history.go(-1)
    })

})(window, document, jQuery)